import { LogHelper } from "../utils/logHelper";

/**
 * 孵化队列任务
 */
export class SpawnQueueTask implements IRoomTask{
  run(room: Room): void {
    if(room.memory.spawnQueue){
      // 孵化重要性排队
      room.memory.spawnQueue.sort((a, b) => {
        if (a.priority && !b.priority) {
          return -1;
        } else if (!a.priority && b.priority) {
          return 1;
        } else {
          return 0;
        }
      });
      for(const creep of room.memory.spawnQueue){
        // 进行孵化
        const spawn = room.find(FIND_MY_SPAWNS)[0];
        if(spawn){
          if(!creep.priority){
            if(spawn.spawning || !room.memory.spawnAllow){
              return;
            }
          }
          const creepName = `${creep.role}_${Game.time}`;
          const result = spawn.spawnCreep(creep.body, creepName, { memory: creep.memory});

          if(result === OK){
            // 如果孵化成功了，从队列中删掉
            room.spawnQueueRemove(creep);
            room.memory.spawnAllow = false;
            return;
          }
          // else if(result === ERR_NOT_ENOUGH_ENERGY){
          //   let need = 0;
          //   creep.body.forEach(b=>{
          //     switch (b) {
          //       case MOVE:
          //         need += 50;
          //         break;
          //       case WORK:
          //         need += 100;
          //         break;
          //       case CARRY:
          //         need += 50;
          //         break;
          //       case ATTACK:
          //         need += 80;
          //         break;
          //       case RANGED_ATTACK:
          //         need += 150;
          //         break;
          //       case HEAL:
          //         need += 250;
          //         break;
          //       case TOUGH:
          //         need += 10;
          //         break;
          //       case CLAIM:
          //         need += 600;
          //         break;
          //     }
          //   });
          //   LogHelper.warn(`孵化队列 房间 [${room.name}] ${creep.role} 生成能量不足 生成creep所需能量 ${need}`);
          // }
          else{
            if(creep.priority){
              return;
            }
          }
        }
      }
    }
  }
}